package com.android.wlw_local.login;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.commonlib.netlib.RemoteService;
import com.android.commonlib.netlib.RequestCallback;
import com.android.commonlib.netlib.RequestParameter;
import com.android.wlw_local.AppApplication;
import com.android.wlw_local.MainActivity;
import com.android.wlw_local.R;
import com.android.wlw_local.dialog.DialogThridUtils;
import com.android.wlw_local.service.SendUtils;
import com.farsunset.cim.sdk.android.CIMPushManager;
import com.farsunset.cim.sdk.android.model.ReplyBody;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * Created by zhuqin on 2018/1/16.
 */

public class LoginActivity extends Activity{
    private TextView title ;
    private TextView right ;
    private TextView commit ;

    private RegistModelEditText username ;
    private RegistModelEditText password ;

    private Dialog mDialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_login);
        title = (TextView) findViewById(R.id.title);
        title.setText("登录");
        right = (TextView) findViewById(R.id.right);
        right.setText("注册");
        commit = (TextView) findViewById(R.id.tv_text);
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(right.getText().toString().trim().equals("注册")){
                    title.setText("注册");
                    right.setText("登录");
                    commit.setText("登录");
                }else {
                    title.setText("登录");
                    right.setText("注册");
                    commit.setText("注册");
                }
            }
        });

        username = (RegistModelEditText) findViewById(R.id.et_phone_num);
        password = (RegistModelEditText) findViewById(R.id.et_password);
        password.setLeftimg(R.drawable.sjd_dl_yzm);
        password.setHint("请输入密码");
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(username.getText().toString().toString())){
                    Toast.makeText(LoginActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(password.getText().toString().toString())){
                    Toast.makeText(LoginActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(commit.getText().toString().trim().equals("登录")){//登录
                    //登录
                    if(CIMPushManager.isConnected(LoginActivity.this)){
                        SendUtils.loginSend(LoginActivity.this,username.getText().toString().toString(),password.getText().toString().toString());
                    }else {
                        CIMPushManager.connect(LoginActivity.this,"120.78.51.39",13131);
                        if(CIMPushManager.isConnected(LoginActivity.this)){
                            SendUtils.loginSend(LoginActivity.this,username.getText().toString().toString(),password.getText().toString().toString());
                        }else {
                            Toast.makeText(LoginActivity.this, "登录失败，请检查网络情况", Toast.LENGTH_LONG).show();
                        }
                    }
                }else {//注册
                    register(username.getText().trim().toString(),password.getText().toString().trim());
                }
            }
        });
    }

    private void register(final String username , final String password){
        ArrayList<RequestParameter> params = new ArrayList<>();
        params.add(new RequestParameter("account",username));
        params.add(new RequestParameter("password",password));
        RemoteService.getInstance().invoke("registerUser", params, new RequestCallback() {
            @Override
            public void onSuccess(String content) {
                //登录
                if(CIMPushManager.isConnected(LoginActivity.this)){
                    CIMPushManager.bindAccount(LoginActivity.this,username);
                }else {
                    CIMPushManager.connect(LoginActivity.this,"120.78.51.39",13131);
                    if(CIMPushManager.isConnected(LoginActivity.this)){
                        SendUtils.loginSend(LoginActivity.this,username,password);
                    }else {
                        Toast.makeText(LoginActivity.this, "登录失败，请检查网络情况", Toast.LENGTH_LONG).show();
                    }
                }
                DialogThridUtils.closeDialog(mDialog);
            }

            @Override
            public void onFail(String errorMessage) {
                DialogThridUtils.closeDialog(mDialog);
                Toast.makeText(LoginActivity.this,"注册失败",Toast.LENGTH_LONG).show();
            }

            @Override
            public void showDialog() {
                mDialog = DialogThridUtils.showWaitDialog(LoginActivity.this, "加载中...", false, true);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void loginCalBack(ReplyBody mLoginEvent) {
        if(mLoginEvent!= null && mLoginEvent.get("checkinfo")!= null && mLoginEvent.get("checkinfo").equals("false")){
            Toast.makeText(LoginActivity.this,"账户不存在，请先注册",Toast.LENGTH_SHORT).show();
            return;
        }
        if(mLoginEvent != null&& mLoginEvent.get("isOnline")!= null  && mLoginEvent.get("isOnline").equals("true")){
            AppApplication.getAppApplication().isOriginOnline = true ;
        }else {
            AppApplication.getAppApplication().isOriginOnline = false ;
        }
        //登录成功 跳转到首页
        UserStore.getInstance().setUserInfo(username.getText().toString().trim(),password.getText().toString().trim());
        //跳转
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        finish();


    }
}
